﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AlertSystem.Business
{
    public partial class Warning_System_Type_Account
    {
        #region Singleton Pattern
        private static object _lock = new object();
        private static List<Warning_System_Type_Account> _instance;
        public static List<Warning_System_Type_Account> GetInstance()
        {
            lock (_lock)
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        using (var context = new DBDataContext(AppSetting.ConnectionString))
                        {
                            _instance = context.Warning_System_Type_Accounts.ToList();
                        }
                    }
                }
            }
            return _instance;
        }
        public static void ReloadData()
        {
            lock (_lock)
            {
                using (var context = new DBDataContext(AppSetting.ConnectionString))
                {
                    _instance = context.Warning_System_Type_Accounts.ToList();
                }
            }
        }
        #endregion Singleton Pattern

        #region static method
        public static Warning_System_Type_Account GetByID(string Warning_System_Type_Account, string system_id, string account_id)
        {
            try
            {
                return GetInstance().Find(p => p.WarningType == Warning_System_Type_Account && p.Account_Id == account_id && p.System_Id == system_id);
            }
            catch
            {
                return null;
            }
        }

        public static Warning_System_Type_Account FindItem(Predicate<Warning_System_Type_Account> predicate)
        {
            try
            {
                return GetInstance().Find(predicate);
            }
            catch
            {
                return null;
            }
        }

        public static IEnumerable< Warning_System_Type_Account> FindItems(Func<Warning_System_Type_Account,bool> predicate)
        {
            try
            {
                return GetInstance().Where(predicate);
            }
            catch
            {
                return Enumerable.Empty<Warning_System_Type_Account>();
            }
        }

        public static bool Any(Func<Warning_System_Type_Account, bool> predicate)
        {
            try
            {
                return GetInstance().Any(predicate);
            }
            catch
            {
                return false;
            }
        }

        public static bool All(Func<Warning_System_Type_Account, bool> predicate)
        {
            try
            {
                return GetInstance().All(predicate);
            }
            catch
            {
                return false;
            }
        }

        public static List<Warning_System_Type_Account> Paging(DBDataContext context, int currentPage, int pageSize, ref int totalRecord, string warningType, string system_id, string account_id)
        {
            var resutl = context.Warning_System_Type_Accounts.AsQueryable();
            if (!string.IsNullOrWhiteSpace(warningType))
            {
                resutl = resutl.Where(p => p.WarningType.Contains(warningType));
            }
            if (!string.IsNullOrWhiteSpace(system_id))
            {
                resutl = resutl.Where(p => p.System_Id.Contains(system_id));
            }
            if (!string.IsNullOrWhiteSpace(account_id))
            {
                resutl = resutl.Where(p => p.Account_Id.Contains(account_id));
            }
            totalRecord = resutl.Count();
            return resutl.Skip(((currentPage - 1) < 0 ? 0 : (currentPage - 1)) * pageSize).Take(pageSize).ToList();
        }
        #endregion static method
    }
}
